﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using AE_Timesheet_Common;
using AE_Timesheet_BLL;
using System.Web.Script.Serialization;

namespace AE_Timesheet_MobileWebservice
{
    /// <summary>
    /// Summary description for Mobile
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Mobile : System.Web.Services.WebService
    {
        #region Objects
        string sServer = string.Empty;
        string sLicenseServer = string.Empty;
        string sSQLUserName = string.Empty;
        string sSQLPassword = string.Empty;
        public string sErrDesc = string.Empty;

        public Int16 p_iDebugMode = DEBUG_ON;

        public const Int16 RTN_SUCCESS = 1;
        public const Int16 RTN_ERROR = 0;
        public const Int16 DEBUG_ON = 1;
        public const Int16 DEBUG_OFF = 0;

        clsProject oProject = new clsProject();
        clsTimeSheet oTimeSheet = new clsTimeSheet();
        clsLog oLog = new clsLog();
        #endregion

        #region WebMethods

        //[WebMethod]
        //[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        //public string MLogin_Authenticate(string sUserName, string sPassword)
        //{
        //    return "";
        //}

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string MGet_ProjectList()
        {
            try
            {
                DataSet ds = oProject.Get_ProjectList();
                if (ds != null && ds.Tables.Count > 0)
                {
                    List<Project> lstProject = new List<Project>();
                    foreach (DataRow r in ds.Tables[0].Rows)
                    {
                        Project _project = new Project();
                        _project.Code = r["PrjCode"].ToString();
                        _project.Name = r["PrjName"].ToString();
                        lstProject.Add(_project);
                    }
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    return js.Serialize(lstProject);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return string.Empty;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string MRead_TimesheetEntry(DateTime dtBeginDate, string sUserName)
        {
            try
            {
                DataSet ds = oTimeSheet.Get_TimesheetEntry(dtBeginDate, sUserName);
                if (ds != null && ds.Tables.Count > 0)
                {
                    List<RetriveTimeSheet> lstTimesheet = new List<RetriveTimeSheet>();
                    foreach (DataRow r in ds.Tables[0].Rows)
                    {
                        RetriveTimeSheet _retriveTimeSheet = new RetriveTimeSheet();
                        _retriveTimeSheet.No = r["No"].ToString();
                        _retriveTimeSheet.IDS = r["IDS"].ToString();
                        _retriveTimeSheet.UserCode = r["UserCode"].ToString();
                        _retriveTimeSheet.Client = r["Client"].ToString();
                        _retriveTimeSheet.PrjCode = r["PrjCode"].ToString();
                        _retriveTimeSheet.OnSite = r["OnSite"].ToString();
                        _retriveTimeSheet.PrjName = r["PrjName"].ToString();
                        _retriveTimeSheet.Status = Convert.ToInt32(r["Status"]);
                        _retriveTimeSheet.SAPB1DB = r["SAPB1DB"].ToString();
                        _retriveTimeSheet.Task = r["Task"].ToString();
                        _retriveTimeSheet.Comments = r["Comments"].ToString();
                        _retriveTimeSheet.Mon = Convert.ToDecimal(r["Mon"]);
                        _retriveTimeSheet.Tue = Convert.ToDecimal(r["Tue"]);
                        _retriveTimeSheet.Wed = Convert.ToDecimal(r["Wed"]);
                        _retriveTimeSheet.Thu = Convert.ToDecimal(r["Thu"]);
                        _retriveTimeSheet.Fri = Convert.ToDecimal(r["Fri"]);
                        _retriveTimeSheet.Sat = Convert.ToDecimal(r["Sat"]);
                        _retriveTimeSheet.Sun = Convert.ToDecimal(r["Sun"]);
                        _retriveTimeSheet.Total = Convert.ToDecimal(r["Total"]);
                        _retriveTimeSheet.Billable = Convert.ToInt32(r["Billable"]);
                        _retriveTimeSheet.ServiceType = r["ServiceType"].ToString();
                        _retriveTimeSheet.U_AI_TimeEndDate = r["U_AI_TimeEndDate"].ToString();
                        _retriveTimeSheet.Phase = r["Phase"].ToString();
                        _retriveTimeSheet.MoreInfo = r["MoreInfo"].ToString();
                        lstTimesheet.Add(_retriveTimeSheet);
                    }
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    return js.Serialize(lstTimesheet);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return string.Empty;
        }

        #endregion

        #region Class

        class Project
        {
            public string Code { get; set; }
            public string Name { get; set; }
        }

        class RetriveTimeSheet
        {
            public string No { get; set; }
            public string IDS { get; set; }
            public string UserCode { get; set; }
            public string Client { get; set; }
            public string PrjCode { get; set; }
            public string OnSite { get; set; }
            public string PrjName { get; set; }
            public Int32 Status { get; set; }
            public string SAPB1DB { get; set; }
            public string Task { get; set; }
            public string Comments { get; set; }
            public decimal Mon { get; set; }
            public decimal Tue { get; set; }
            public decimal Wed { get; set; }
            public decimal Thu { get; set; }
            public decimal Fri { get; set; }
            public decimal Sat { get; set; }
            public decimal Sun { get; set; }
            public decimal Total { get; set; }
            public Int32 Billable { get; set; }
            public string ServiceType { get; set; }
            public string U_AI_TimeEndDate { get; set; }
            public string Phase { get; set; }
            public string MoreInfo { get; set; }
        }

        #endregion
    }
}
